using System.Collections;
using UnityEngine;

public static class MonoBehaviourExtensions
{
	private static IEnumerator waitAndExecuteCoroutine;

	public static void ResetTransformation(this Transform trans)
	{
		trans.position = Vector3.zero;
		trans.localRotation = Quaternion.identity;
		trans.localScale = Vector3.one;
	}

	public static Coroutine WaitAndExecute<T>(this MonoBehaviour mb, float time, SingleParameterDelegate<T> d, T param)
	{
		if (time <= 0f)
		{
			if (d != null)
			{
				d(param);
			}
			return null;
		}
		return mb.StartCoroutine(mb.DoWaitAndExecute(time, d, param));
	}

	public static IEnumerator DoWaitAndExecute<T>(this MonoBehaviour mb, float time, SingleParameterDelegate<T> d, T param)
	{
		yield return new WaitForSeconds(time);
		if (d != null)
		{
			d(param);
		}
	}

	public static IEnumerator DoWaitAndExecuteEnum(this MonoBehaviour mb, float time, VoidDelegate d)
	{
		yield return new WaitForSeconds(time);
		if (d != null)
		{
			d();
		}
	}

	public static Coroutine WaitAndExecute(this MonoBehaviour mb, float time, VoidDelegate d)
	{
		if (time <= 0f)
		{
			if (d != null)
			{
				d();
			}
			return null;
		}
		return mb.StartCoroutine(mb.DoWaitAndExecuteEnum(time, d));
	}

	private static ReflectionAction.ActionBuilderParams GetWaitAndExecuteParams(string runActionName, string stopActionName, object[] runParams, object[] stopParams)
	{
		ReflectionAction.ActionBuilderParams result = default(ReflectionAction.ActionBuilderParams);
		result.typeName = "MonoBehaviourExtensions";
		result.runMethodName = runActionName;
		result.runMethodParams = runParams;
		result.stopMethodName = stopActionName;
		result.stopMethodParams = stopParams;
		return result;
	}

	public static void WaitAndExecuteAction(this MonoBehaviour mb, float time, QueuableAction.OnComplete onComplete)
	{
		if (time <= 0f)
		{
			if (onComplete != null)
			{
				onComplete();
			}
		}
		else
		{
			waitAndExecuteCoroutine = mb.DoWaitAndExecute(time, onComplete.Invoke);
			mb.StartCoroutine(waitAndExecuteCoroutine);
		}
	}

	public static void StopWaitAndExecuteAction(this MonoBehaviour mb, QueuableAction.OnComplete onComplete)
	{
		if (!(mb == null) && waitAndExecuteCoroutine != null)
		{
			mb.StopCoroutine(waitAndExecuteCoroutine);
			if (onComplete != null)
			{
				onComplete();
			}
			waitAndExecuteCoroutine = null;
		}
	}

	private static IEnumerator DoWaitAndExecute(this MonoBehaviour mb, float time, VoidDelegate d)
	{
		yield return new WaitForSeconds(time);
		if (d != null)
		{
			d();
		}
		waitAndExecuteCoroutine = null;
	}
}
